package com.ojoin.ojoin.eb_spider.check;

import com.ojoin.ojoin.eb_spider.dao.CatchDataResultMapper;
import com.ojoin.ojoin.eb_spider.dao.MeinianUserMapper;
import com.ojoin.ojoin.eb_spider.domain.AlipayCheckException;
import com.ojoin.ojoin.eb_spider.domain.AssistCatchData;
import com.ojoin.ojoin.eb_spider.domain.EmailMessage;
import com.ojoin.ojoin.eb_spider.listening.MeidiMeiMeiJiaYuanCatchDateListening;
import com.ojoin.ojoin.eb_spider.model.CatchDataResult;
import com.ojoin.ojoin.eb_spider.model.MeinianUser;
import com.ojoin.ojoin.eb_spider.service.CookieService;
import com.ojoin.ojoin.eb_spider.service.EmailMessageService;
import com.ojoin.ojoin.eb_spider.service.ISLDataCatchBaseService;
import com.ojoin.ojoin.eb_spider.service.catchdata.MeidiMeiMeiJiaYuanDataCatchServiceImpl;
import com.ojoin.ojoin.eb_spider.utils.Config;
import com.ojoin.ojoin.eb_spider.utils.SpringUtilsSpider;
import com.ojoin.ojoin.eb_spider.utils.WebServiceUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.supplyloan_core.web.domain.CatchDataStartMessage;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformInfo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class Dispatcher {
    private Logger log = LoggerFactory.getLogger(Dispatcher.class);

    @Autowired
    private MeidiMeiMeiJiaYuanDataCatchServiceImpl meidiMeiMeiJiaYuanDataCatchService;

    @Autowired
    private EmailMessageService emailMessageService;

    @Autowired
    private MeinianUserMapper meinianUserMapper;

    @Autowired
    private CatchDataResultMapper catchDataResultMapper;


    @Autowired
    @Qualifier("cookieServiceDB")
    //cookieServiceFile
    //cookieServiceDict
    private CookieService cookieService;

    public static final Map<String, EmailMessage> emailMessage = Collections
            .synchronizedMap(new HashMap<String, EmailMessage>());

    public static final Map<String, AlipayCheckException> stands = Collections
            .synchronizedMap(new HashMap<String, AlipayCheckException>());

    /**
     * 保持session在线
     */

    //@Scheduled(cron = "0 0/2 * * * ?")
    public void CookieKeepOnline() throws Exception {
        try {

            List<TbSlPlatformUser> list = new ArrayList<TbSlPlatformUser>();
            TbSlPlatformUser user = WebServiceUtils.getPlatformUser(101);

            list.add(user);

            cookieService.cookieKeeyOnline(list);

        } catch (Exception e) {
            LogUtils.error(log, e);
            sendException(e);
        }
    }
    /**
     * 监控支付宝账号
     *
     * @throws Exception
     */

    /**
     * 报告 早10：00 晚17：00
     *
     * @throws Exception
     */


    public void sendException(Exception e) {

        EmailMessage em = new EmailMessage(e, TimeUtils.nowUTC());

        boolean isSuccess = emailMessageService.sendMailException(em);

        if (!isSuccess) {
            LogUtils.error(log, "发送邮件失败");
        }
    }


    public TbSlPlatformUser getUser() {
        TbSlPlatformUser user = new TbSlPlatformUser();

        user.setId(10000000);
        user.setIdOfTbMerchantUser(75);
        user.setBindAlipay(1 + "");

        return user;
    }

    /**
     * 一年执行一次
     */

    //@Scheduled(fixedRate = 1000*60*60*24*365)
    public void persistRecord() {
        CatchDataStartMessage ca = new CatchDataStartMessage();
        TbSlPlatformUser tmall = new TbSlPlatformUser();

        tmall.setUsername("AW1201037314");
        tmall.setPassword("Sr123456");
                /*
                 * tmall.setPassword("M54321"); tmall.setUsername("tjmf_00@sina.com");
		 */

        tmall.setPlatformName(TbSlPlatformInfo.SUPPLIER_SUNING);

        TbSlPlatformInfo info = new TbSlPlatformInfo();
        info.setLoginUrl("http://scs.suning.com/sps/portal/showPortalPage.action");

        ca.setPlatformUser(tmall);
        ca.setPlatformInfo(info);
        ca.setCatchType(CatchDataStartMessage.LOCALHOST);

        AssistCatchData as = new AssistCatchData(ca);

        MeidiMeiMeiJiaYuanCatchDateListening lis = new MeidiMeiMeiJiaYuanCatchDateListening(meidiMeiMeiJiaYuanDataCatchService, as);
        meidiMeiMeiJiaYuanDataCatchService.Listenings.add(lis);

        meidiMeiMeiJiaYuanDataCatchService.catchData(as);
    }

    /**
     * 一年执行一次
     */

    //@Scheduled(cron = "0 0 7,17,23 * * ?")
    //@Scheduled(fixedRate = 1000 * 60 * 60 * 24 * 365)
    public void MeiNian() {
        int m = 5;
        do {
            List<MeinianUser> list = meinianUserMapper.selectCatchFail(Config.MEI_NIAN);
            HashSet<MeinianUser> mus = new HashSet(list);

            if (null != mus && mus.size() > 0) {
                m--;
                for (MeinianUser mu : mus) {
                    if (mu.getId() != 19 && mu.getId() != 32 && mu.getId() != 5 && mu.getId() != 24 && mu.getId() != 14) {
                        continue;
                    }
                    CatchDataResult cr = new CatchDataResult();
                    cr.setMuId(mu.getId());
                    try {
                        MeiNianOne(mu);
                        cr.setStatus(1);

                    } catch (Exception e) {
                        cr.setStatus(-1);
                        cr.setCause(e.getMessage());
                        log.error("", e);
                    } finally {
                        catchDataResultMapper.deleteByMidAndTime(cr);
                        catchDataResultMapper.insert(cr);
                    }

                }
            } else {
                m = -1;
                log.info("all user catch success");
            }
        } while (m < 0);

    }

    public void MeiNianOne(MeinianUser mu) throws Exception {
        CatchDataStartMessage ca = new CatchDataStartMessage();
        TbSlPlatformUser meinian = new TbSlPlatformUser();

        meinian.setUsername(mu.getUsername());
        meinian.setPassword(mu.getPassword());

        meinian.setPlatformName("meinian");

        meinian.setCompanyName(mu.getCompanyName());

        TbSlPlatformInfo info = new TbSlPlatformInfo();
        info.setLoginUrl("http://meinian.cn/Research.aspx");

        ca.setPlatformUser(meinian);


        ca.setPlatformInfo(info);
        ca.setCatchType(CatchDataStartMessage.LOCALHOST);


        ISLDataCatchBaseService service = SpringUtilsSpider
                .getBeanByClassName("MeiNianDataCatchServiceImpl2");

        AssistCatchData as = new AssistCatchData(ca);
        service.catchData(as);


    }

    //@Scheduled(fixedRate = 1000*60*60*24*365)
    public void CiMing() throws Exception {
        CatchDataStartMessage ca = new CatchDataStartMessage();
        TbSlPlatformUser meinian = new TbSlPlatformUser();

        meinian.setUsername("BJZXYBKJ");
        meinian.setPassword("111111");

        meinian.setPlatformName("CiMing");

        meinian.setCompanyName("CiMing");

        TbSlPlatformInfo info = new TbSlPlatformInfo();
        info.setLoginUrl("http://book.ciming.com/hr/BJZXYBKJ");

        ca.setPlatformUser(meinian);


        ca.setPlatformInfo(info);
        ca.setCatchType(CatchDataStartMessage.LOCALHOST);


        ISLDataCatchBaseService service = SpringUtilsSpider
                .getBeanByClassName("CiMingDataCatchServiceImpl");

        AssistCatchData as = new AssistCatchData(ca);
        service.catchData(as);

    }

}
